package com.github.gaojh.example.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.gaojh.example.bean.DemoBean;
import com.github.gaojh.example.service.DemoService;
import com.github.gaojh.example.websocket.WebSocketContext;
import com.github.gaojh.example.websocket.WebSocketHandler;
import com.github.gaojh.ioc.annotation.Autowired;
import com.github.gaojh.ioc.annotation.Controller;
import com.github.gaojh.mvc.annotation.*;
import com.github.gaojh.server.http.HttpResponse;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 高建华
 * @date 2019-04-01 11:11
 */
@Controller
public class DemoController {


    @Autowired
    private DemoService demoService;

    @Autowired
    private WebSocketHandler webSocketHandler;

    @GET("/cmd")
    public String cmd(@RequestParam String cmd){
        JSONObject json = new JSONObject();
        json.putOnce("msgType","CMD");
        Map<String,String> map = new HashMap<>();
        map.put("platform","1");
        map.put("cmd",cmd);
        json.putOnce("data",map);
        WebSocketContext.getChannel("169.254.253.196").writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(json)));
        return "OK";
    }

    @RequestMapping("/hello")
    public String hello(@RequestParam String name){
        return demoService.getName(name);
    }

    @RequestMapping("/demo")
    public Object demo(@RequestBody DemoBean demoBean){
        return demoBean;
    }

    @RequestMapping("/h2/*")
    public String h2(){
        return "h2";
    }

    @POST("/test")
    public void test(HttpResponse httpResponse){
        httpResponse.sendJson(new DemoBean().setName("gaojh"));
    }

}
